<?php
/**
 * Created by PhpStorm.
 * User: 张辉
 * Date: 2018/7/11
 * Time: 14:29
 */
namespace app\crontab\controller\spider;
use app\common\service\PidFile;
use app\crontab\controller\CrontabCommon;

/**
 * Class SpiderCountAction 文章抓取相关统计
 */
class Count extends CrontabCommon
{
    /**
     * 初始化方法
     */
    protected function _initialize(){
        //先执行父类初始化
        parent::_initialize();
    }


    public function index()
    {
        if ( !PidFile::createPidFile('SpiderCount', 1 ) ) {
            echo '进程已启动足够数量'.PHP_EOL;
            return;
        }

        if ( IS_WIN ){
            $this->winRun();
        } else {
            $this->linuxRun();
        }

    }

    /**
     * win下处理程序
     */
    protected function winRun()
    {
        $i = 0;
        $key_word_list = $this->getKeyWord();
        $SpiderArticleCountModel = new SpiderArticleCountModel();
        // 获取近七日文章列表
        $article_list = $SpiderArticleCountModel->getSpiderArticleList();
        foreach ( $key_word_list as $key => $value ){
            $i++;
            echo $i;
//            $info = $SpiderArticleCountModel->getKeyWordCount( $SpiderArticleModel, $key );
            $info = $SpiderArticleCountModel->getKeyWordCountV2( $key, $article_list  );
            $info['type'] = $value;
            $SpiderArticleCountModel->add_or_update( $info );
        }
    }

    /**
     * linux处理程序
     */
    protected function linuxRun()
    {
        $key_word_list = $this->getKeyWord();

        $SpiderArticleCountModel = new SpiderArticleCountModel();
        $article_list = $SpiderArticleCountModel->getSpiderArticleList();

        // 计算总关键词
        $count = count( $key_word_list );
        $article_count = count( $article_list );

        // 按文章数设置进程数
        $process = ceil($article_count/10000 ) ?: 1;
        // 每个进程数据数量
        $limit = ceil($count/$process);
        // 数据切割分组
        $all_data_chunk = array_chunk( $key_word_list, $limit, true );

        // 主进程池
        $pid_list = array();
        foreach ( $all_data_chunk as $value ){
            $i = 0;
            $pid = pcntl_fork();
            if( $pid==-1 ){
                die("can not fork");
            }elseif ( $pid ) {
                $pid_list[] = $pid;
            }else {
                // 执行代码
            	$SpiderArticleCountModel = new SpiderArticleCountModel();
//            	$SpiderArticleModel = new SpiderArticleModel();

            	// 获取近七日文章列表
                $article_list = $SpiderArticleCountModel->getSpiderArticleList();

                foreach ( $value as $k => $v ) {
//                    $data = $SpiderArticleCountModel->getKeyWordCount( $SpiderArticleModel, $k  );
                    $data = $SpiderArticleCountModel->getKeyWordCountV2( $k, $article_list  );
                    $data['type'] = $v;


                    $SpiderArticleCountModel->add_or_update( $data );
                    $i++;
                    echo $i;
                }
                exit();
            }
        }

        while(count($pid_list) > 0)
        {
            $myId = pcntl_waitpid(-1, $status, WNOHANG);
            foreach($pid_list as $key => $pid)
            {
                if($myId == $pid) unset($pid_list[$key]);
            }
        }
    }


    /**
     * 获取文章检索关键词及类型
     * @return array
     */
    protected function getKeyWord()
    {
        $key_word_list = [];

        // 组合标签词
        $tag_model = TagModel::getInstance();
        $tag_list = $tag_model->muti_get_data_by_where();
        if ( $tag_list ){
            $tag_list = array_fill_keys( array_filter( array_column( $tag_list,'name' ) ), SpiderArticleCountModel::KEY_WORD_TYPE_NO_TOKEN );
            $key_word_list = array_merge( $key_word_list,  $tag_list );
        }

        // 组合token简称、中文名称、英文名称
        $token_model = CoinRuntimeModel::getInstance();
        $token_list = $token_model->muti_get_data_by_where();

        if ( $token_list ){
            $token_list = array_fill_keys( array_filter(
                array_merge(
                    array_column($token_list,'symbol'),
                    array_column($token_list,'cht_name'),
                    array_column($token_list,'en_name')
                )
            ),SpiderArticleCountModel::KEY_WORD_TYPE_TOKEN
            );

            $key_word_list = array_merge( $key_word_list,  $token_list );
        }

        return $key_word_list;
    }










}